package app

import (
	"encoding/json"
	"github.com/gorilla/websocket"
	"im_project/configs"
	"log"
	"time"
)

func SendMessage() {
	for {
		select {
		case msg := <-configs.MessagesChan:

			data, err := json.Marshal(msg)
			if err != nil {
				continue
			}
			configs.ClientsSync.Range(func(_, client interface{}) bool {
				if client.(*configs.Client).Conn.WriteMessage(websocket.TextMessage, data) != nil {
					log.Println(err)
				}
				return true
			})

		case <-time.After(11 * time.Millisecond):
			continue

		default:
			time.Sleep(time.Second)
			continue
		}

	}
}
